package com.g4.shoppingsingle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingsingle.mapper.DdAddressInfoMapper;
import com.g4.shoppingsingle.pojo.DdAddressInfo;
import com.g4.shoppingsingle.pojo.DdUsersInfo;
import com.g4.shoppingsingle.service.IDdAddressInfoService;
import com.g4.shoppingsingle.service.IDdUsersInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@Service
public class DdAddressInfoServiceImpl extends ServiceImpl<DdAddressInfoMapper, DdAddressInfo> implements IDdAddressInfoService {

    @Autowired(required = false)
    private DdAddressInfoMapper ddAddressInfoMapper;

    @Autowired
    private IDdUsersInfoService iDdUsersInfoService;

    /**
     * 查找地址
     *
     * @return
     */
    @Override
    public List<DdAddressInfo> selectById() {
        //获取个人信息
        DdUsersInfo me = iDdUsersInfoService.getMe();
        Integer userId = me.getId();
        //通过
        QueryWrapper<DdAddressInfo> wrapper = new QueryWrapper<>();
        wrapper.in("userId", userId);


        return ddAddressInfoMapper.selectList(wrapper);
    }

    /**
     * 增加地址
     *
     * @param address
     * @param userId
     */

    @Override
    public void addAddressInfo(String address, int userId) {
        DdAddressInfo info = new DdAddressInfo();
        info.setUserId(userId);
        info.setAddress(address);


        ddAddressInfoMapper.insert(info);

    }

    /**
     * 删除地址
     *
     * @param id
     */
    @Override
    public void deleteAddress(int id) {
        ddAddressInfoMapper.deleteById(id);


    }

    /**
     * 更新地址
     *
     * @param addressId
     * @param assress
     * @param userId
     */

    @Override
    public void updateAddress(int addressId, String assress, int userId) {
        DdAddressInfo info = new DdAddressInfo();

        info.setUserId(userId);
        info.setId(addressId);
        info.setAddress(assress);
        ddAddressInfoMapper.updateById(info);


    }


}
